/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = wtc_comm.h                                        */
/*  NOTE      =                                                    */
/*  DATE      = 2015/08/13 by songbb                               */
/*******************************************************************/

#ifndef __WTC_COMM_H_
#define __WTC_COMM_H_

#include <sys/time.h>

#include "frlog.h"
#include "frreport_comm.h"

#include "wtc_core.h"

#define APM_WTC_DBG 0
#define APM_WTC_ENABLE_FRLOG 0
#define APM_ORA_ENABLE_COLOR_PRINT 1    // 是否启用彩色打印

#if APM_ORA_ENABLE_COLOR_PRINT
#define KRED        "\x1B[0;31m"
#define KSALMON     "\x1B[1;31m"
#define KGREEN      "\x1B[0;32m"
#define KLIMEGREEN  "\x1B[1;32m"
#define KYELLOW     "\x1B[0;33m"
#define KORANGE     "\x1B[0;34m"
#define KBLUE       "\x1B[1;34m"
#define KMAGENTA    "\x1B[0;35m"
#define KPINK       "\x1B[1;35m"
#define KCYAN       "\x1B[0;36m"
#define RESET       "\x1B[0m"
#else
#define KRED        ""
    #define KSALMON     ""
    #define KGREEN      ""
    #define KLIMEGREEN  ""
    #define KYELLOW     ""
    #define KORANGE     ""
    #define KBLUE       ""
    #define KMAGENTA    ""
    #define KPINK       ""
    #define KCYAN       ""
    #define RESET       ""
#endif

#define APM_WTC_HEADER_SMAGIC 0x73903842
#define APM_WTC_HEADER_MAGIC  0x91039858
#define APM_WTC_LLE_MAGIC  0x7cafeba7

/* 标识符变量的初始化值 */
#define APM_WTC_FLAG_UNSET -1

#define APM_WTC_HASH_SIZE 100      //!< TuxedoWTC模块哈希桶的最大尺寸

/* 通用头长度 */
#define APM_WTC_PKT_HEADER_LEN 32
#define APM_WTC_TCH_SIZE 12

/* Opcode */
#define APM_WTC_GWNW_XATMI_CALL 1                   // req
#define APM_WTC_GWNW_XATMI_REPLY 2                  // resp
#define APM_WTC_GWNW_XATMI_FAILURE 3                //
#define APM_WTC_GWNW_XATMI_CONNECT 4                // req
#define APM_WTC_GWNW_XATMI_DATA 5                   //
#define APM_WTC_GWNW_XATMI_DISCON 6                 // req
#define APM_WTC_GWNW_XATMI_PREPARE 7                //
#define APM_WTC_GWNW_XATMI_READY 8                  //
#define APM_WTC_GWNW_XATMI_COMMIT 9                 // req
#define APM_WTC_GWNW_XATMI_DONE 10                  // resp
#define APM_WTC_GWNW_XATMI_COMPLETE 11              //
#define APM_WTC_GWNW_XATMI_ROLLBACK 12              // req
#define APM_WTC_GWNW_XATMI_HEURISTIC 13             //
#define APM_WTC_GWNW_XATMI_PRE_NW_ACALL1 14         // req
#define APM_WTC_GWNW_XATMI_PRE_NW_ACALL1_RPLY 15    // resp
#define APM_WTC_GWNW_XATMI_PRE_NW_ACALL2 16         // req
#define APM_WTC_GWNW_XATMI_PRE_NW_ACALL2_RPLY 17    // resp
#define APM_WTC_GWNW_XATMI_PRE_NW_ACALL3 18         // req
#define APM_WTC_GWNW_XATMI_PRE_NW_ACALL3_RPLY 19    // resp
#define APM_WTC_GWNW_XATMI_PRE_NW_LLE 20            // req
#define APM_WTC_GWNW_XATMI_PRE_NW_LLE_RPLY 21       // resp
#define APM_WTC_GWNW_XATMI_SEC_EXCHG_RQST 22        //
#define APM_WTC_GWNW_XATMI_SEC_EXCHG 22             //
#define APM_WTC_GWNW_XATMI_DMQUERY 23               //
#define APM_WTC_GWNW_XATMI_RDONLY 24                //

#define WTC_OPCODE2STR(x) ((x==APM_WTC_GWNW_XATMI_CALL)?"CALL": \
                                 ((x==APM_WTC_GWNW_XATMI_REPLY)?"REPLY": \
                                 ((x==APM_WTC_GWNW_XATMI_FAILURE)?"FAILURE": \
                                 ((x==APM_WTC_GWNW_XATMI_CONNECT)?"CONNECT": \
                                 ((x==APM_WTC_GWNW_XATMI_DATA)?"DATA": \
                                 ((x==APM_WTC_GWNW_XATMI_DISCON)?"DISCON": \
                                 ((x==APM_WTC_GWNW_XATMI_PREPARE)?"PREPARE": \
                                 ((x==APM_WTC_GWNW_XATMI_READY)?"READY": \
                                 ((x==APM_WTC_GWNW_XATMI_COMMIT)?"COMMIT": \
                                 ((x==APM_WTC_GWNW_XATMI_DONE)?"DONE": \
                                 ((x==APM_WTC_GWNW_XATMI_COMPLETE)?"COMPLETE": \
                                 ((x==APM_WTC_GWNW_XATMI_ROLLBACK)?"ROLLBACK": \
                                 ((x==APM_WTC_GWNW_XATMI_HEURISTIC)?"HEURISTIC": \
                                 ((x==APM_WTC_GWNW_XATMI_PRE_NW_ACALL1)?"PRE_NW_ACALL1": \
                                 ((x==APM_WTC_GWNW_XATMI_PRE_NW_ACALL1_RPLY)?"PRE_NW_ACALL1_RPLY": \
                                 ((x==APM_WTC_GWNW_XATMI_PRE_NW_ACALL2)?"PRE_NW_ACALL2": \
                                 ((x==APM_WTC_GWNW_XATMI_PRE_NW_ACALL2_RPLY)?"PRE_NW_ACALL2_RPLY": \
                                 ((x==APM_WTC_GWNW_XATMI_PRE_NW_ACALL3)?"PRE_NW_ACALL3": \
                                 ((x==APM_WTC_GWNW_XATMI_PRE_NW_ACALL3_RPLY)?"PRE_NW_ACALL3_RPLY": \
                                 ((x==APM_WTC_GWNW_XATMI_PRE_NW_LLE)?"PRE_NW_LLE": \
                                 ((x==APM_WTC_GWNW_XATMI_PRE_NW_LLE_RPLY)?"PRE_NW_LLE_RPLY": \
                                 ((x==APM_WTC_GWNW_XATMI_SEC_EXCHG_RQST)?"SEC_EXCHG_RQST": \
                                 ((x==APM_WTC_GWNW_XATMI_SEC_EXCHG)?"SEC_EXCHG": \
                                 ((x==APM_WTC_GWNW_XATMI_DMQUERY)?"DMQUERY": \
                                 ((x==APM_WTC_GWNW_XATMI_RDONLY)?"RDONLY":"UNKNOWN")))))))))))))))))))))))))

#define WTC_TCBTYPE2STR(x) ((x==APM_WTC_TCB_TRAN)?"TRAN": \
                                 ((x==APM_WTC_TCB_WS)?"WS": \
                                 ((x==APM_WTC_TCB_UNSOL)?"UNSOL": \
                                 ((x==APM_WTC_TCB_COMPOS_HDR)?"COMPOS_HDR": \
                                 ((x==APM_WTC_TCB_COMPOS_FML)?"COMPOS_FML": \
                                 ((x==APM_WTC_TCB_TDOM)?"TDOM": \
                                 ((x==APM_WTC_TCB_ROUTE)?"ROUTE": \
                                 ((x==APM_WTC_TCB_TDOM_TRAN)?"TDOM_TRAN": \
                                 ((x==APM_WTC_TCB_AAA_TCM)?"AAA_TCM": \
                                 ((x==APM_WTC_TCB_CALLOUT)?"CALLOUT": \
                                 ((x==APM_WTC_TCB_TDOM_VALS)?"TDOM_VALS": \
                                 ((x==APM_WTC_TCB_CODESET_TCM)?"CODESET_TCM": \
                                 ((x==APM_WTC_TCB_META_TCM)?"META_TCM": \
                                 ((x==APM_WTC_TCB_TDOM_SEC)?"TDOM_SEC": \
                                 ((x==APM_WTC_TCB_TEST_TCM)?"TEST_TCM": \
                                 ((x==APM_WTC_TCB_PKCS7_TCM)?"PKCS7_TCM": \
                                 ((x==APM_WTC_TCB_QS_TCM)?"QS_TCM": \
                                 ((x==APM_WTC_TCB_PROP_TCM)?"PROP_TCM":"UNKNOWN"))))))))))))))))))

#define WTC_DIRECT2STR(x) ((x==FR_DIRECT_UNKNOWN)?"FR_DIRECT_UNKNOWN": \
                                 ((x==FR_DIRECT_KNOWN)?"FR_DIRECT_KNOWN": \
                                 ((x==FR_DIRECT_SRV2CLIENT)?"FR_DIRECT_SRV2CLIENT": \
                                 ((x==FR_DIRECT_CLIENT2SRV)?"FR_DIRECT_CLIENT2SRV":"UNKNOWN"))))


#define APM_WTC_RPCRQ_HINT 0
#define APM_WTC_RPCRP_HINT 1
#define APM_WTC_MTTYP_HINT 2
#define APM_WTC_TPINITTYPE_HINT 3
#define APM_WTC_COMPOS_HINT 4
#define APM_WTC_TM_HINT 5
#define APM_WTC_TRAN_HINT 6
#define APM_WTC_WS_HINT 7
#define APM_WTC_UNSOL_HINT 8
#define APM_WTC_CMPS_HDR_HINT 9
#define APM_WTC_TGIOP_HINT 10
#define APM_WTC_WSRPCRQ_HINT 11
#define APM_WTC_ROUTE_HINT 12
#define APM_WTC_PROP_HINT 13
#define APM_WTC_CALLOUT_HINT 14
#define APM_WTC_TDOM_VALS_HINT 15
#define APM_WTC_CARRAY_HINT 16
#define APM_WTC_STRING_HINT 17
#define APM_WTC_FML_HINT 18
#define APM_WTC_VIEW_HINT 19
#define APM_WTC_X_OCTET_HINT 20
#define APM_WTC_X_C_TYPE_HINT 21
#define APM_WTC_X_COMMON_HINT 22
#define APM_WTC_FML32_HINT 23
#define APM_WTC_VIEW32_HINT 24
#define APM_WTC_XML_HINT 25
#define APM_WTC_MBSTRING_HINT 26

#define APM_WTC_FLD_SHORT 0
#define APM_WTC_FLD_LONG 1
#define APM_WTC_FLD_CHAR 2
#define APM_WTC_FLD_FLOAT 3
#define APM_WTC_FLD_DOUBLE 4
#define APM_WTC_FLD_STRING 5
#define APM_WTC_FLD_CARRAY 6
#define APM_WTC_FLD_INT 7
#define APM_WTC_FLD_DECIMAL 8
#define APM_WTC_FLD_PTR 9
#define APM_WTC_FLD_FML32 10
#define APM_WTC_FLD_VIEW32 11
#define APM_WTC_FLD_MBSTRING 12

#define APM_WTC_RPCRQ_STRING "rpcrqst"
#define APM_WTC_RPCRP_STRING "rpcrply"
#define APM_WTC_MTTYP_STRING "metamsg"
#define APM_WTC_TPINITTYPE_STRING "TPINIT"
#define APM_WTC_COMPOS_STRING "COMPOS"
#define APM_WTC_ITCM_STRING "ITCM"
#define APM_WTC_TM_STRING "TM"
#define APM_WTC_TRAN_STRING "TRAN"
#define APM_WTC_WS_STRING "WS"
#define APM_WTC_UNSOL_STRING "UNSOL"
#define APM_WTC_CMPS_HDR_STRING "CMPS_HDR"
#define APM_WTC_TGIOP_STRING "TGIOP"
#define APM_WTC_WSRPCRQ_STRING "wsrpcrq"
#define APM_WTC_ROUTE_STRING "ROUTE"
#define APM_WTC_PROP_STRING "PROP"
#define APM_WTC_CALLOUT_STRING "CALLOUT"
#define APM_WTC_TDOM_VALS_STRING "TDOM_VALS"
#define APM_WTC_CARRAY_STRING "CARRAY"
#define APM_WTC_STRING_STRING "STRING"
#define APM_WTC_FML_STRING "FML"
#define APM_WTC_VIEW_STRING "VIEW"
#define APM_WTC_X_OCTET_STRING "X_OCTET"
#define APM_WTC_X_C_TYPE_STRING "X_C_TYPE"
#define APM_WTC_X_COMMON_STRING "X_COMMON"
#define APM_WTC_FML32_STRING "FML32"
#define APM_WTC_VIEW32_STRING "VIEW32"
#define APM_WTC_XML_STRING "XML"
#define APM_WTC_MBSTRING_STRING "MBSTRING"

#define APM_DTS_REPORT_MODULE_NAME "dts"
#define APM_DTS_REPORT_SESSION_USERROLE APM_REPORT_KEY_SESSION"."APM_DTS_REPORT_MODULE_NAME".userrole"
#define APM_DTS_REPORT_SESSION_CLIENT_DOMAINID APM_REPORT_KEY_SESSION"."APM_DTS_REPORT_MODULE_NAME".client_domainid"
#define APM_DTS_REPORT_SESSION_SERVICE_DOMAINID APM_REPORT_KEY_SESSION"."APM_DTS_REPORT_MODULE_NAME".service_domainid"
#define APM_DTS_REPORT_REQ_SERVICE_NAME APM_REPORT_KEY_REQ"."APM_DTS_REPORT_MODULE_NAME".service.name"
#define APM_DTS_REPORT_REQ_SERVICE_METHOD APM_REPORT_KEY_REQ"."APM_DTS_REPORT_MODULE_NAME".service.method"
#define APM_DTS_REPORT_REQ_ARG_SIZE APM_REPORT_KEY_REQ"."APM_DTS_REPORT_MODULE_NAME".service.arg.size"
#define APM_DTS_REPORT_REQ_ARG_DATA APM_REPORT_KEY_REQ"."APM_DTS_REPORT_MODULE_NAME".service.arg."
#define APM_DTS_REPORT_RESP_ARG_SIZE APM_REPORT_KEY_RESP"."APM_DTS_REPORT_MODULE_NAME".service.return.size"
#define APM_DTS_REPORT_RESP_ARG_DATA APM_REPORT_KEY_RESP"."APM_DTS_REPORT_MODULE_NAME".service.return."

#if APM_WTC_DBG
    #define WTC_PRINT(...) printf(__VA_ARGS__)
    #define WTC_PRINT_DETAIL(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(__VA_ARGS__))
    #define WTC_PRINT_DBG(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KGREEN),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define WTC_PRINT_FATAL(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KORANGE),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define WTC_PRINT_EMERG(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KRED),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define WTC_PRINT_ALERT(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KSALMON),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define WTC_PRINT_CRIT(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KMAGENTA),\
                            printf(" "__VA_ARGS__),\
                            printf(RESET))
    #define WTC_PRINT_ERR(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KPINK),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define WTC_PRINT_WARN(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KYELLOW),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define WTC_PRINT_NOTICE(...) (printf("[%s, Line:%u, %s()]",\
                        __FILE__,__LINE__,__func__),\
                        printf(KLIMEGREEN),\
                        printf(" "__VA_ARGS__),\
                        printf(RESET))
    #define WTC_PRINT_INFO(...) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KBLUE),\
                            printf(" "__VA_ARGS__),\
                            printf(RESET))
     #define PRINT_HEX_STRING(a,b,c) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),wtc_print_hex_string(b,c),printf("]\n"),\
                            printf(RESET))
    #define WTC_PRINT_PACKET_SPECIFIED(tag, packet, offset, len) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KORANGE),\
                            printf(tag":["),wtc_print_packet_specified(packet,offset,len),printf("]\n"),\
                            printf(RESET))

    #define WTC_PRINT_PACKET_CURRENT(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),wtc_print_packet_current(b),printf("]\n"),\
                            printf(RESET))

    #define WTC_PRINT_PACKET(a,b) (printf("[%s, Line:%u, %s()] ",\
                            __FILE__,__LINE__,__func__),\
                            printf(KCYAN),\
                            printf(a":["),wtc_print_packet(b),printf("]\n"),\
                            printf(RESET))
    #define WTC_PRINT_TIMEVAL(a,b) (printf("[%s, Line:%u, %s()]",\
                            __FILE__,__LINE__,__func__),\
                            printf(KGREEN),\
                            printf(" %s", a), \
                             wtc_print_timeval(b),\
                             printf(RESET))
#else
    #define WTC_PRINT(...)
    #define WTC_PRINT_DETAIL(...)
    #define WTC_PRINT_DBG(...)
    #define WTC_PRINT_FATAL(...)
    #define WTC_PRINT_EMERG(...)
    #define WTC_PRINT_ALERT(...)
    #define WTC_PRINT_CRIT(...)
    #define WTC_PRINT_ERR(...)
    #define WTC_PRINT_WARN(...)
    #define WTC_PRINT_NOTICE(...)
    #define WTC_PRINT_INFO(...)
    #define PRINT_HEX_STRING(a,b,c)
    #define WTC_PRINT_PACKET_SPECIFIED(tag, packet, offset, len)
    #define WTC_PRINT_PACKET_CURRENT(a,b)
    #define WTC_PRINT_PACKET(a,b)
    #define WTC_PRINT_TIMEVAL(a,b)
#endif

#if APM_WTC_ENABLE_FRLOG
    #define WTC_FRLOG_ERROR(...) FRLOG_ERROR_DETAIL(...)
#else
#define WTC_FRLOG_ERROR(...)
#endif

#define WTC_MALLOCK_ERR WTC_PRINT_EMERG("can not malloc memory\n"); \
                          FRLOG_ERROR_DETAIL("can not malloc memory");

void wtc_free(void **pp_buffer);

void wtc_print_timeval(struct timeval st_timestamp);

void wtc_bin_to_hex_string(char *p_inbuff
                            , int i_len
                            , char *p_outbuff);

void wtc_print_hex_string(char *p_buff
                        , int i_len);

void wtc_print_packet_specified(L7_wtc_packet *p_st_wtc_packet
                                , int i_pos
                                , int i_len);

void wtc_print_packet_current(L7_wtc_packet *p_st_wtc_packet);

void wtc_print_packet(L7_wtc_packet *p_st_wtc_packet);

int wtc_packet_is_in_fragment(L7_wtc_packet *p_st_wtc_packet);

int wtc_report_msg_data_req(Conntrack *p_st_conn
                            , L7session_wtc *p_st_sess
                            , L7_wtc_packet *p_st_wtc_packet
                            , Frreport_body_data *p_st_body
                            , Frhash_header *p_st_hash_report);

int wtc_report_msg_data_resp(Conntrack *p_st_conn
                            , L7session_wtc *p_st_sess
                            , L7_wtc_packet *p_st_wtc_packet
                            , Frreport_body_data *p_st_body
                            , Frhash_header *p_st_hash_report);

void wtc_free_stmt(L7_wtc_stmt **pp_st_wtc_stmt);

void wtc_init_stmt(Conntrack *p_st_conn
                , L7_wtc_stmt *p_st_wtc_stmt);

int wtc_analyze_direction_by_opcode(unsigned int ui_opcode);

#endif
